#include "studentwidget.h"
#include "ui_studentwidget.h"
#include<QFileDialog>
#include<QPainter>
#include<QDebug>
#include<QSqlError>
#include<QByteArray>
#include<QBuffer>
#include<QStringList>
#include<vector>


StudentWidget::StudentWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StudentWidget)
{
    ui->setupUi(this);

    SetStuId("1");

    MySql.ConnectSql("192.168.64.128");
    MySql.SetStuId(StuId);

    InitHeadPhoto();
    InitInfo();
    InitScore();
    InitComment();
    InitStage();

    connect(ui->pb_SetInfo,SIGNAL(clicked()),this,SLOT(ConnectModifyWidget()));
    connect(ui->pb_SetScore,SIGNAL(clicked()),this,SLOT(ConnectScore()));
    connect(ui->pb_SetComment,SIGNAL(clicked()),this,SLOT(ConnectComment()));

}

StudentWidget::~StudentWidget()
{
    delete ui;
}

void StudentWidget::InitHeadPhoto()
{
    ui->lb_header->setPixmap(QPixmap(":/image/head1.png").scaled(ui->lb_header->width(),ui->lb_header->height()));
    headerPhoto = *ui->lb_header->pixmap();

    QSqlQuery query(db);

    QString Key = StuId;    //主键
    QString table = "image"; //表名
    QString column = "image";//列名

    QString getPhoto = "select " + column + " from " + table + " where id = " + Key;
    //qDebug() << getPhoto << endl;
    if(query.exec(getPhoto))
    {
        if(query.next())
        {
            QByteArray imageInfo = query.value("image").toByteArray();
            QPixmap m;
            m.loadFromData(imageInfo);
            ui->lb_header->setPixmap(m);
            headerPhoto = *ui->lb_header->pixmap();
        }
    }
}

void StudentWidget::InitInfo()
{
    ui->lb_info->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    QStringList list = {"","","","","","",""};

    MySql.GetInfoFromSql(list);

    QString form = "姓名:" + list.value(0) + "\n"
                  +"性别:" + list.value(1) + "\n"
                  +"出生日期:" + list.value(2) + "\n"
                  +"所在城市:" + list.value(3) + "\n"
                  +"户口所在地:" + list.value(4) + "\n"
                  +"电话号码:" + list.value(5) + "\n"
                  +"电子邮箱" + list.value(6) + "\n";

    ui->lb_info->setText(form);
}

void StudentWidget::InitScore()
{
    ui->lb_Score->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    std::vector<std::pair<std::string,int>> v;
    QString ScoreInfo;

    MySql.GetScoreFromSql(v);

    for(auto score : v)
    {
        ScoreInfo += QString::fromStdString(score.first) + QString::number(score.second) + "\n";
    }
    ui->lb_Score->setText(ScoreInfo);

}

void StudentWidget::InitComment()
{
    ui->lb_Comment->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    QString comment = "教师评价:\n";

    MySql.GetCommentFromSql(comment);

    ui->lb_Comment->setText(comment);
}

void StudentWidget::InitStage()
{
    ui->lb_Stage->setAlignment(Qt::AlignLeft | Qt::AlignTop);

    QString stage;

    MySql.GetStageFromSql(stage);

    ui->lb_Stage->setText(stage);
}

void StudentWidget::SetHeadPhoto(QMouseEvent *e)
{
    if(e->button() == Qt::LeftButton)
    {
        QPoint labolPos = ui->lb_header->mapFromParent(e->pos());
        if(ui->lb_header->rect().contains(labolPos))
        {
            QString path = QFileDialog::getOpenFileName(this,"获取一张图片",":/");
            QPixmap pixmap(path);
            if(path.size() != 0)
            {
                QPixmap image(ui->lb_header->width(),ui->lb_header->width());
                image.fill(Qt::transparent);
                QPainter painter(&image);
                painter.setBrush(pixmap.scaled(ui->lb_header->width(),ui->lb_header->height(),Qt::KeepAspectRatioByExpanding,Qt::SmoothTransformation));

                painter.drawEllipse(0,0,ui->lb_header->width(),ui->lb_header->height());
                ui->lb_header->setPixmap(image);
                headerPhoto = *ui->lb_header->pixmap();

                //将最新的头像置入数据库
                BackPhotoToSql();

            }
        }
    }

}

void StudentWidget::BackPhotoToSql()
{
    QByteArray imageInfo;
    QBuffer buffer(&imageInfo);
    buffer.open(QIODevice::WriteOnly);
    headerPhoto.toImage().save(&buffer,"PNG");


    QSqlQuery query(db);
    QString Email = StuId;
    QString SavePhoto = "update image set image = :imageData where id = " + Email;

    query.prepare(SavePhoto);
    query.bindValue(":imageData",imageInfo);

    query.exec();
}

void StudentWidget::mousePressEvent(QMouseEvent *e)
{
    SetHeadPhoto(e);
}

void StudentWidget::mouseMoveEvent(QMouseEvent *e)
{
    mouseMoveEventByHead(e);

}

void StudentWidget::mouseMoveEventByHead(QMouseEvent *e)
{
    QPoint labolPos = ui->lb_header->mapFromParent(e->pos());
    if(ui->lb_header->rect().contains(labolPos))
    {
        ui->lb_header->setPixmap(QPixmap(":/image/add.png").scaled(ui->lb_header->width(),ui->lb_header->height()));
    }
    else
    {
        ui->lb_header->setPixmap(headerPhoto);
    }
}

void StudentWidget::paintEvent(QPaintEvent *e)
{
    QPainter paint(this);
    QPixmap image(":/image/Background-1.png");
    paint.drawPixmap(0,0,this->width(),this->height(),image);
}

void StudentWidget::SetStuId(const QString& StuId)
{
    this->StuId = StuId;
}

QString StudentWidget::Id()
{
    return this->StuId;
}

void StudentWidget::ConnectModifyWidget()
{
    StuModifyWidget.show();

}

void StudentWidget::ConnectComment()
{
    StuCommentWidget.show();
}

void StudentWidget::ConnectScore()
{
    StuScoreWidget.show();
}

